//
//  SearchViewController.m
//  PigMarket
//
//  Created by mac on 2019/6/11.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SearchViewController.h"
#import "SearchView.h"
#import "SearchResultViewController.h"

@interface SearchViewController ()

/**
 searchView
 */
@property (nonatomic , strong) SearchView *searchView;

/**
 无结果view
 */
@property (nonatomic , strong) UIView *noResultView;
/**
 无结果Lab
 */
@property (nonatomic , strong) UILabel *noResultLab;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
@end

@implementation SearchViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method

- (void)setUpNav {
    [self.navigationItem setHidesBackButton:YES];
    UIButton *cancelButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 26)];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [cancelButton setTitleColor:[UIColor colorWithHexString:@"#656565"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
    self.navigationItem.rightBarButtonItem = rightItem;

    self.searchView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 70, NavigationBarFrame.size.height);
    self.navigationItem.titleView = self.searchView;
}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.noResultView];
    [self.noResultView addSubview:self.noResultLab];
}

- (void) initLayout {

    [self.noResultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(100);
    }];
    [self.noResultLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.noResultView.mas_centerX);
        make.centerY.mas_equalTo(self.noResultView.mas_centerY);
        make.height.mas_equalTo(20);
    }];
}

- (void) initAction {
   
    //搜索
    @weakify(self);
    [self.searchView setSearchEndBlock:^(NSString *searchText) {
        
        @strongify(self);
        switch (self.searchType) {
            case SearchTypeAddNewFriend:
                [self searchNewFriendRequestWithText:searchText];
                break;
            case SearchTypeFriendList:
                [self searchRequestWithText:searchText];

                break;
            default:
                break;
        }
    }];
}

- (void)initData {
    
}

#pragma mark - delegate

#pragma mark - target action
- (void)leftButtonClick {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method
//搜索好友列表中的好友
- (void)searchRequestWithText:(NSString *)text {
    
    @weakify(self);
    [[[PigMarketApi sharedInstance] myFriendsListWithMemberId:[UserInfoDataProvider sharedInstance].userInfo.memberId nickName:text currentPage:1] subscribeNext:^(FriendsList * _Nullable friendList) {
        @strongify(self);
        [self.dataArr removeAllObjects];
        if (friendList.list.count == 0 || [JTool iSNull:friendList.list]) {
            //为空  无搜索结果
            self.noResultView.hidden = NO;
        } else {
            self.noResultView.hidden = YES;
            [self.dataArr addObjectsFromArray:friendList.list];
            SearchResultViewController *vc = [[SearchResultViewController alloc] init];
            vc.dataArr = self.dataArr;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

//添加好友 搜索好友
- (void)searchNewFriendRequestWithText:(NSString *)text {
    @weakify(self);
    [[[PigMarketApi sharedInstance] searchAddFriendWithSearchText:text] subscribeNext:^(SearchAddFriendList * _Nullable friendList) {
        @strongify(self);
        [self.dataArr removeAllObjects];
        if (friendList.dataList.count == 0 || [JTool iSNull:friendList.dataList]) {
            //为空  无搜索结果
            self.noResultView.hidden = NO;
        } else {
            self.noResultView.hidden = YES;
            [self.dataArr addObjectsFromArray:friendList.dataList];
            SearchResultViewController *vc = [[SearchResultViewController alloc] init];
            vc.dataArr = self.dataArr;
            [self.navigationController pushViewController:vc animated:YES];
        }
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}
#pragma mark - getter and setter

- (SearchView *)searchView {
    if (!_searchView) {
        _searchView = [[SearchView alloc] init];
        _searchView.searchBar.searchBarStyle = UISearchBarStyleMinimal;
        _searchView.searchBar.placeholder = @"昵称/手机号";
    }
    return _searchView;
}
- (UIView *)noResultView {
    if (!_noResultView) {
        _noResultView = [[UIView alloc] init];
        _noResultView.backgroundColor = [UIColor whiteColor];
        _noResultView.hidden = YES;
    }
    return _noResultView;
}
- (UILabel *)noResultLab {
    if (!_noResultLab) {
        _noResultLab = [[UILabel alloc] init];
        _noResultLab.textColor = [UIColor colorWithHexString:@"#ACACAC"];
        _noResultLab.font = [UIFont systemFontOfSize:14];
        _noResultLab.textAlignment = NSTextAlignmentCenter;
        _noResultLab.text = @"该用户不存在";
    }
    return _noResultLab;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end
